package com.ihai.spring.boot.sample.httpclient;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.AsyncClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.web.client.AsyncRequestCallback;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * DESC
 *
 * @author Adolphus
 * @date 2019/3/23
 */
@SpringBootApplication
public class HttpclientApplication {
    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext = SpringApplication.run(HttpclientApplication.class,args);
        RestTemplate commonClient = applicationContext.getBean("commonClient", RestTemplate.class);
        AtomicInteger count = new AtomicInteger(0);
        for(int i=0; i< 100; i++) {
            String response = commonClient.getForObject("https://www.makeapie.com/chart/list?builtinTags[]=category-work&sortBy=rank&pageSize=1000&pageNumber="+(i+1)+"&author=all", String.class);
            ObjectMapper mapper = new ObjectMapper();
            JsonNode json = mapper.readValue(response, JsonNode.class);
            JsonNode charts = json.get("data").get("charts");
            if(charts.size() == 0){
                System.exit(0);
            }
            charts.forEach(node -> {
                File dir = new File("E:/makeapie/"+count.incrementAndGet() + "-" + node.get("cid").asText() + "-" + node.get("title").asText());
                if(dir.exists()){
                    try {
                        FileUtils.deleteDirectory(dir);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                dir.mkdirs();
                FileOutputStream out = null;
                try {
                    ResponseEntity<byte[]> image = commonClient.getForEntity(node.get("thumbnailURL").asText(), byte[].class);
                    File img = new File(dir, node.get("cid").asText() + ".png");
                    try {
                        out = new FileOutputStream(img);
                        out.write(image.getBody(), 0, image.getBody().length);
                        out.flush();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (out != null) {
                            try {
                                out.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
                try {
                    String script = mapper.readValue(commonClient.getForObject("https://www.makeapie.com/chart/get/"+node.get("cid").asText(), String.class), JsonNode.class).get("data").get("code").asText();
                    File js = new File(dir,"script.js");
                    try {
                        out = new FileOutputStream(js);
                        out.write(script.getBytes(StandardCharsets.UTF_8),0,script.getBytes(StandardCharsets.UTF_8).length);
                        out.flush();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        if (out!=null){
                            try {
                                out.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
        System.exit(0);
    }
}
